home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / LSEEK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  816 b   |  30 lines

  1. /* lseek.c --- p 497 */
  2. #include <stdio.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5. main()
  6. {
  7.     int fhandle, count,
  8.     long curpos;
  9.     unsigned char buffer[20];
  10.                     /* Open the file "autoexec.bat." */
  11.     if ( (fhandle = open("c:\\autoexec.bat", O_RDONLY)) == -1)
  12.     {
  13.         printf("open failed");
  14.         exit(1);
  15.     }
  16.             /* Go to end of file using "lseek" and SEEK_END */
  17.     curpos = lseek(fhandle, OL, SEEK_END);
  18.     printf("End of file in 'autoexec.bat' is %ld bytes from beginning\n",
  19.                                 curpos);
  20.                 /* Move back 20 bytes, read the last 20 bytes
  21.              * and print the characters that were read in. */
  22.     lseek(fhandle, -20L, SEEK_CUR);
  23.     if ((count = read(fhandle, buffer, 20)) == -1)
  24.     {
  25.         perror("read error");
  26.         exit(1);
  27.     }
  28.     printf("The last 20 characters in 'autoexec.bat' are:\n");
  29.     write(1, buffer, count);
  30. }